package restaraunt.rating.project;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

public class RateFood extends Activity implements android.view.View.OnClickListener {

	// Declare Variables
		// (None yet)
	
	// Declare Controls
	EditText tNam, tCom, tRst;
	RatingBar rRat;
	Button bSub;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rate_food);
		
		// Init. Variables
			// (None yet)
		
		// Init. Controls
		tNam = (EditText) findViewById(R.id.etFoodName);	
		rRat = (RatingBar) findViewById(R.id.rbFoodRating);
		tCom = (EditText) findViewById(R.id.etComments);
		tRst = (EditText) findViewById(R.id.etRestaraunt);
		
		bSub = (Button) findViewById(R.id.btSubmit);
		bSub.setOnClickListener(this);
		
		// Init. Listeners
		// Listeners moved to seperate onClick
	}

	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.btSubmit :
			// Part 1: Apply information to database
			boolean submitSuccess = true;
			try {
				String foodName = tNam.getText().toString();
				String foodRate = String.valueOf(rRat.getRating());
				String foodComm = tCom.getText().toString();
				String restName = tRst.getText().toString();
				
				FoodRating entry = new FoodRating(RateFood.this);
				entry.open();
				entry.createEntry(foodName, foodRate, foodComm, restName);
				entry.close();
			} catch (Exception e) {
				submitSuccess = false;
			} finally {

				if (submitSuccess) {
					Dialog dlg = new Dialog(this);
					dlg.setTitle("Food Rating App");
					TextView tv = new TextView(this);
					tv.setText("Your food rating has been successfully submitted.");
					dlg.setContentView(tv);
					dlg.show();
				}
			}
			break;
		}
		// Part 2: Synchronize database w/ online account, if possible. 
		
		// Part 3: Send message showing success/failure
		
		// Part 4: Close activity, show current database
		Intent openMenu = new Intent("restaraunt.rating.project.VIEWFOOD");
		startActivity(openMenu);
		finish();
	}
}
